//
//  ReleaseDeliciousFoodTwoController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ReleaseDeliciousFoodTwoController.h"
#import "ReleaseDeliciousFoodThereController.h"

@interface ReleaseDeliciousFoodTwoController ()<UITableViewDelegate,UITableViewDataSource,ReleaseDeliciousFoodTwoCellDelegate>

/**listView*/
@property (nonatomic,strong)UITableView * listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
@end

@implementation ReleaseDeliciousFoodTwoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavItems];
    
    [self.view addSubview:self.listView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
-(void)nextAction{
    ReleaseFoodManage.seasoningArray = self.dataSoure;
    [self.navigationController pushViewController:[ReleaseDeliciousFoodThereController new] animated:YES];
}
-(void)setNavItems{
    self.title = @"发布（2/3）";
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setBackgroundImage:IMAGE_NAMED(@"back") forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    [leftBtn sizeToFit];

    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(16);
    [rightBtn sizeToFit];

    kWeakSelf(weakSelf);
    [rightBtn addActionHandler:^{
        NSMutableArray * oneArray = self.dataSoure[0];
        NSMutableArray * twoArray = self.dataSoure[1];
        NSMutableArray * thereArray = self.dataSoure[2];
        NSMutableArray * fourArray = self.dataSoure[3];

        if (oneArray.count == 0) {
            ShowHubMessage(@"请添加主料");
            return ;
        }
        if (twoArray.count == 0) {
            ShowHubMessage(@"请添加辅料");
            return ;
        }
        if (thereArray.count == 0) {
            ShowHubMessage(@"请添加调料");
            return ;
        }
        if (fourArray.count == 0) {
            ShowHubMessage(@"请添加做法");
            return ;
        }

        [weakSelf nextAction];
    }];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    
}


#pragma mark --UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * array = self.dataSoure[section];
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReleaseDeliciousFoodTwoCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ReleaseDeliciousFoodTwoCell class])];
    ReleaseDeliciousFoodTwoBottomCell * bomCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ReleaseDeliciousFoodTwoBottomCell class])];
    
    if (indexPath.section == self.dataSoure.count - 1) {
        bomCell.delegate = self;
        bomCell.indexPath = indexPath;
        bomCell.model = self.dataSoure[indexPath.section][indexPath.row];
        
        return bomCell;
    }
    
    cell.indexPath = indexPath;
    ReleaseDeliciousFoodModel * model = self.dataSoure[indexPath.section][indexPath.row];
    cell.model = model;
    cell.delegate=self;
        
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 50)];
    view.backgroundColor = KWhiteColor;
    
    UIView * dotView = UIView.new;
    dotView.backgroundColor = UIColorFromRGB(0xFFE864);
    ViewRadius(dotView, 8);
    
    UILabel * titlLaebl = UILabel.new;
    titlLaebl.textColor = UIColorFromRGB(0x273323);
    titlLaebl.font = BOLDSYSTEMFONT(18);
    
    if (section == 0) {
        titlLaebl.text = @"添加主料";
    }else if (section == 1){
        titlLaebl.text = @"辅料";
    }else if (section == 2){
        titlLaebl.text = @"调料";
    }else if (section == 3){
        titlLaebl.text = @"做法";
    }
    
    [view addSubview:dotView];
    [view addSubview:titlLaebl];
    
    [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(16);
    }];
    [titlLaebl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(dotView.mas_right).offset(16);
        make.centerY.mas_equalTo(0);
    }];

    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSArray * array = self.dataSoure[section];
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
    
    view.backgroundColor = KWhiteColor;
    
    UIView * lineView = UIView.new;
    lineView.backgroundColor = SeparatorColor;
    lineView.hidden = array.count==0;
    
    UIButton * button = UIButton.new;
    [button setTitleColor: UIColorFromRGB(0x009966) forState:UIControlStateNormal];
    button.titleLabel.font = BOLDSYSTEMFONT(14);
    
    if (section == 0) {
        [button setTitle:@"再添加一种食材" forState:UIControlStateNormal];
    }else if (section == 1){
        [button setTitle:@"再添加一种辅料" forState:UIControlStateNormal];
    }else if (section == 2){
        [button setTitle:@"再添加一种辅料" forState:UIControlStateNormal];
    }else if (section == 3){
        [button setTitle:@"再添加一步" forState:UIControlStateNormal];
    }
    
    [view addSubview:button];
    [view addSubview:lineView];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.left.mas_equalTo(16);
    }];

    kWeakSelf(weakSelf);
    
    [button addActionHandler:^{
        ReleaseDeliciousFoodModel * model = [[ReleaseDeliciousFoodModel alloc] init];
        switch (section) {
            case 0:
                model.leftPlaceholder = @"请输入主料名称";
                model.rightPlaceholder = @"请输入用量";
                model.leftTitle = @"食材:";
                model.rightTitle = @"用量:";
                break;
            case 1:
                model.leftPlaceholder = @"请输入辅料名称";
                model.rightPlaceholder = @"请输入用量";
                model.leftTitle = @"辅料:";
                model.rightTitle = @"用量:";
            
            break;
            case 2:
                model.leftPlaceholder = @"请输入调料名称";
                model.rightPlaceholder = @"请输入用量";
                model.leftTitle = @"调料:";
                model.rightTitle = @"用量:";
            
            break;
            case 3:
                model.leftPlaceholder = @"详细描述";
                model.rightPlaceholder = @"";
                model.leftTitle = @"";
                model.rightTitle = @"";
            
            break;
            default:
                break;
        }
        [weakSelf.dataSoure[section] addObject:model];
        [weakSelf.listView reloadData];
        if (section == weakSelf.dataSoure.count - 1) {
            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:[self.listView numberOfRowsInSection:section]-1 inSection:section];

            [weakSelf.listView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
    }];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}
#pragma mark --ReleaseDeliciousFoodTwoCellDelegate
-(void)leftTextFieldCellText:(NSString *)text indexPath:(NSIndexPath *)indexPath{
    ReleaseDeliciousFoodModel * model = self.dataSoure[indexPath.section][indexPath.row];
    model.leftName = text;
}
-(void)rightTextFieldCellText:(NSString *)text indexPath:(NSIndexPath *)indexPath{
    ReleaseDeliciousFoodModel * model = self.dataSoure[indexPath.section][indexPath.row];
    model.rightName = text;
}
-(UITableView *)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        [_listView registerClass:[ReleaseDeliciousFoodTwoCell class] forCellReuseIdentifier:NSStringFromClass([ReleaseDeliciousFoodTwoCell class])];
        [_listView registerClass:[ReleaseDeliciousFoodTwoBottomCell class] forCellReuseIdentifier:NSStringFromClass([ReleaseDeliciousFoodTwoBottomCell class])];
        _listView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 150)];
        _listView.separatorColor = SeparatorColor;
    }
    return _listView;
}


-(NSMutableArray<NSMutableArray *> *)dataSoure{
    if (!_dataSoure) {
        
        _dataSoure =[NSMutableArray array];
        [_dataSoure addObject:NSMutableArray.new];
        [_dataSoure addObject:NSMutableArray.new];
        [_dataSoure addObject:NSMutableArray.new];
        [_dataSoure addObject:NSMutableArray.new];

    }
    return _dataSoure;
}

@end

@interface ReleaseDeliciousFoodTwoCell ()<UITextFieldDelegate>
/**<#属性#>*/
@property (nonatomic,strong)UITextField*leftTextfield;
/**<#属性#>*/
@property (nonatomic,strong)UITextField*rightTextfield;
/**名称*/
@property (nonatomic,strong)UILabel*leftLabel;
/**重量*/
@property (nonatomic,strong)UILabel*rightLabel;
@end

@implementation ReleaseDeliciousFoodTwoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addSubview:self.leftLabel];
        [self addSubview:self.rightLabel];
        [self addSubview:self.leftTextfield];
        [self addSubview:self.rightTextfield];

        [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(16);
            make.bottom.mas_equalTo(-16);
            make.width.mas_equalTo(40);
        }];
        [self.leftTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.leftLabel);
            make.left.mas_equalTo(self.leftLabel.mas_right).offset(5);
            make.right.mas_equalTo(self.rightLabel.mas_left).offset(-5);
            make.width.mas_equalTo(self.rightTextfield);
        }];
        [self.rightTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
        }];
        [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(40);
           make.right.mas_equalTo(self.rightTextfield.mas_left).offset(-5);
        }];

    }
    return self;
}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}
-(void)setModel:(ReleaseDeliciousFoodModel *)model{
    _model = model;
    self.leftTextfield.placeholder = model.leftPlaceholder;
    self.rightTextfield.placeholder = model.rightPlaceholder;
    self.leftTextfield.text = model.leftName;
    self.rightTextfield.text = model.rightName;
    
    self.leftLabel.text = model.leftTitle;
    self.rightLabel.text = model.rightTitle;
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.leftTextfield) {
        if ([self.delegate respondsToSelector:@selector(leftTextFieldCellText:indexPath:)]) {
            [self.delegate leftTextFieldCellText:textField.text indexPath:self.indexPath];
        }
    }
    if (textField == self.rightTextfield) {
        if ([self.delegate respondsToSelector:@selector(rightTextFieldCellText:indexPath:)]) {
            [self.delegate rightTextFieldCellText:textField.text indexPath:self.indexPath];
        }
    }
}
#pragma mark 懒加载

-(UILabel*)leftLabel{
    if(!_leftLabel){
        _leftLabel=[UILabel new];
        _leftLabel.textColor = UIColorFromRGB(0x959E97);
        _leftLabel.font = SYSTEMFONT(16);
        _leftLabel.numberOfLines=0;
        _leftLabel.text = @"豆腐";
    }
    return _leftLabel;
}
-(UILabel*)rightLabel{
    if(!_rightLabel){
        _rightLabel=[UILabel new];
        _rightLabel.textColor = UIColorFromRGB(0x959E97);
        _rightLabel.font = SYSTEMFONT(16);
        _rightLabel.text = @"500克";
    }
    return _rightLabel;
}
-(UITextField*)leftTextfield{
    if(!_leftTextfield){
        _leftTextfield=[UITextField new];
        _leftTextfield.placeholder = @"输入材料名称";
        _leftTextfield.placeholderFont = SYSTEMFONT(16);
        _leftTextfield.placeholderColor = UIColorFromRGB(0x959E97);
        _leftTextfield.delegate = self;
    }
    return _leftTextfield;
}
-(UITextField*)rightTextfield{
    if(!_rightTextfield){
        _rightTextfield=[UITextField new];
        _rightTextfield.placeholder = @"输入用量";
        _rightTextfield.placeholderFont = SYSTEMFONT(16);
        _rightTextfield.placeholderColor = UIColorFromRGB(0x959E97);
        _rightTextfield.delegate = self;
        _rightTextfield.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _rightTextfield;
}
@end


@interface ReleaseDeliciousFoodTwoBottomCell ()<UITextFieldDelegate>
/**<#属性#>*/
@property (nonatomic,strong)UILabel*topLabel;
/**<#属性#>*/
@property (nonatomic,strong)UITextField*textField;
@end

@implementation ReleaseDeliciousFoodTwoBottomCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)createUI{
    [self addSubview:self.topLabel];
    [self addSubview:self.textField];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.bottom.mas_equalTo(-16);
        make.top.mas_equalTo(self.topLabel.mas_bottom).offset(10);
    }];
}
-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    self.topLabel.text = [NSString stringWithFormat:@"第%ld步",(long)indexPath.row + 1];
}
-(void)setModel:(ReleaseDeliciousFoodModel *)model{
    _model = model;
    self.textField.text = model.leftName;
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(leftTextFieldCellText:indexPath:)]) {
        [self.delegate leftTextFieldCellText:textField.text indexPath:self.indexPath];
    }
}
-(UILabel*)topLabel{
    if(!_topLabel){
        _topLabel=[UILabel new];
        _topLabel.textColor = UIColorFromRGB(0x273323);
        _topLabel.font = SYSTEMFONT(16);
        _topLabel.text = @"第1步";
    }
    return _topLabel;
}
-(UITextField*)textField{
    if(!_textField){
        _textField=[UITextField new];
        _textField.placeholder = @"详细描述";
        _textField.placeholderFont = SYSTEMFONT(16);
        _textField.placeholderColor = UIColorFromRGB(0x959E97);
        _textField.delegate = self;

    }
    return _textField;
}
@end
